package com.yxp423.ssmempl.service.impl;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxp423.ssmempl.bean.Role;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.bean.UserRole;
import com.yxp423.ssmempl.controller.ConstController;
import com.yxp423.ssmempl.mapper.DeptMapper;
import com.yxp423.ssmempl.mapper.RoleMapper;
import com.yxp423.ssmempl.mapper.UserMapper;
import com.yxp423.ssmempl.mapper.UserRoleMapper;
import com.yxp423.ssmempl.service.IRoleService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-05-28 10:52
 * @Version: 1.0
 */
@Service
public class RoleServiceImpl implements IRoleService {

	Cache<String, List> roleListCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 5);


	@Autowired
	RoleMapper roleMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	DeptMapper deptMapper;
	@Autowired
	UserMapper userMapper;


	@Override
	public AjaxResult getRole() {
		AjaxResult result = new AjaxResult();

		if (ObjectUtil.isEmpty(roleListCache.get("roleListCache"))) {

			QueryWrapper<Role> wrapper = new QueryWrapper<>();
			List<Role> roleList = roleMapper.selectList(wrapper.orderByAsc("role_id"));

			result.setSuccess(true);
			result.setData(roleList);
			roleListCache.put("roleListCache", roleList);

		} else {

			result.setSuccess(true);
			result.setMsg("成功");
			result.setData(roleListCache.get("roleListCache"));
		}

		return result;
	}

	@Override
	public AjaxResult getRoleByUser(User user) {

		AjaxResult result = new AjaxResult();
		List<UserRole> roleList = userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id", user.getUserId().toString()));

		for (UserRole userRole : roleList) {

			Role role = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_no", userRole.getRoleNo()));
			userRole.setRoleName(role.getRoleName());
		}

		if (roleList.isEmpty()) {

			result.setSuccess(false);
			result.setMsg("无数据");
			return result;
		} else {

			result.setSuccess(true);
			result.setData(roleList);
			result.setMsg("共有" + roleList.size() + "条数据");
		}

		return result;
	}

	@Override
	@Transactional(rollbackFor = RuntimeException.class)
	public AjaxResult saveUserRoles(User user, String roles) {

		AjaxResult result = new AjaxResult();

//		List<UserRole> userRoleList = userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id", user.getUserId().toString()));

		try {
			String[] rolesArr = roles.split(",");

			int deleteFlag = userRoleMapper.delete(new QueryWrapper<UserRole>().eq("user_id", user.getUserId().toString()));

			for (String s : rolesArr) {

				UserRole userRole = new UserRole();

				userRole.setUserId(user.getUserId().toString());
				userRole.setRoleNo(s);
				userRoleMapper.insert(userRole);

			}
//			int updateFlag = deptMapper.update(dept, new QueryWrapper<Dept>().eq("dept_code", dept.getDeptCode()));

			String deptCode = user.getDeptCode();
			int i = userMapper.updateDept(deptCode, user.getUserId());
			if (i > 0) {
				result.setMsg("修改部门成功");
				result.setSuccess(true);
			}


			result.setSuccess(true);
			result.setMsg("成功");

			new ConstController().cacheClean();

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("错误" + e.toString());
		}


		return result;
	}
}
